package bit.springblogdemo.common.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;

import java.security.Key;
import java.util.Map;

@Slf4j
public class JwtUtils {

    private static String SCERET_STRING="L0l0dlFYQmNZdjlqZWFRVHRJYWxCN2ZMVGVLcWRWdVhpd0dXMkd1eC9Yaz0=";
//    private static Key key= Keys.hmacShaKeyFor(SCERET_STRING.getBytes(StandardCharsets.UTF_8));
    private static Key key= Keys.hmacShaKeyFor(Decoders.BASE64.decode(SCERET_STRING));

    public static String getToken(Map<String, Object> claims){
        String compact = Jwts.builder().setClaims(claims).signWith(key).compact();
        return compact;
    }

    public static Claims parseToken(String token){
        JwtParser build = Jwts.parserBuilder().setSigningKey(key).build();
        Claims claims=null;
        try{
            claims=build.parseClaimsJws(token).getBody();
        }catch(Exception e){
            log.error("token解析失败，token:"+token);
        }

       return claims;
    }

    public static void main(String[] args) {
        String token="eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiemhhbmdzYW4iLCJpZCI6MTJ9.Id4stLuS0hFT-GzkIuG2_aVc4yIUbJKGXMPTuGJbb0M";
        Claims claims=parseToken(token);
        System.out.println(claims);

    }
}
